package com.easy.work.function.mall.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.base.syslib.Logs;
import com.base.syslib.pulltorefresh.PullToRefreshBase;
import com.base.syslib.pulltorefresh.PullToRefreshListView;
import com.base.syslib.util.DensityUtil;
import com.base.syslib.util.MessageTools;
import com.easy.work.R;
import com.easy.work.app.AppSession;
import com.easy.work.app.BaseActivity;
import com.easy.work.function.mall.adapter.ExchangeRecordAdapter;
import com.easy.work.function.mall.bean.ExchangeRecordBean;
import com.easy.work.http.BaseResponse;
import com.easy.work.http.HttpClient;
import com.easy.work.http.request.ExchangeRecordRequest;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.easy.work.R.id.listView;

/**
 * @author zhangfeng
 * @date 2017/12/19
 * @desc 兑换记录
 */

public class ExchangeRecordActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener<ListView>, AdapterView.OnItemClickListener {

    private static final String TAG = ExchangeRecordActivity.class.getSimpleName();

    @BindView(listView)
    PullToRefreshListView refreshListView;

    private ExchangeRecordAdapter mAdapter;
    /**
     * 暂无记录
     */
    private ImageView imgNoData;
//    private int page = 0;

    public static void goExchangeRecordActivity(Context ctx) {
        Intent intent = new Intent(ctx, ExchangeRecordActivity.class);
        ctx.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exchange_record);
        ButterKnife.bind(this);
        initTitle("兑换记录");
        imgNoData = (ImageView) findViewById(R.id.img_no_data);
        initListView();

        AppSession.setPullFormartRefreshTime(refreshListView);
        refreshListView.doPullRefreshing(true, 0);
    }

    /**
     * ListView初始化
     */
    private void initListView() {
        refreshListView.setPullLoadEnabled(true);
        refreshListView.setPullRefreshEnabled(true);
        refreshListView.setOnRefreshListener(this);

        ListView listView = refreshListView.getRefreshableView();
        listView.setDivider(new ColorDrawable(0xffF5F5F5));
        listView.setDividerHeight(DensityUtil.dip2px(this, 10));
        listView.setOnItemClickListener(this);

        List<ExchangeRecordBean.ListEntity> list = new ArrayList<>();
//        // 测试数据
//        for (int i = 0; i < 10; i++) {
//            ExchangeRecordBean.ListEntity entity = new ExchangeRecordBean.ListEntity();
//            entity.goodsName = "联系手动阀";
//            entity.status = 1;
//            list.add(entity);
//        }
        mAdapter = new ExchangeRecordAdapter(this, 1, list);
        listView.setAdapter(mAdapter);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        mAdapter.clear();
        getRecordList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        getRecordList();
    }

    /**
     * 获取兑换记录
     */
    private void getRecordList() {
        final ExchangeRecordRequest request = new ExchangeRecordRequest();
        request.userId = AppSession.getInstance().getUserId();
        Logs.e(TAG, "userId:" + request.userId);
        request.start = mAdapter.getCount();
        request.limit = 10;
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                refreshListView.onPullUpRefreshComplete();
                refreshListView.onPullDownRefreshComplete();
                handleData(response);
            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                refreshListView.onPullUpRefreshComplete();
                refreshListView.onPullDownRefreshComplete();
                MessageTools.showToast(getApplicationContext(), msg);
            }
        });
    }

    /**
     * 数据处理
     */
    private void handleData(BaseResponse response) {
        ExchangeRecordBean bean = (ExchangeRecordBean) response;
        if (bean != null && bean.list != null && !bean.list.isEmpty()) {
            imgNoData.setVisibility(View.GONE);
            mAdapter.addMoreData(bean.list);
        } else {
            if (mAdapter.getCount() == 0) {
                MessageTools.showToast(getApplicationContext(), "暂无数据");
                imgNoData.setVisibility(View.VISIBLE);
            } else {
                MessageTools.showToast(getApplicationContext(), "没有更多数据了");
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // CommitOrderActivity.start(ExchangeRecordActivity.this);
//         SelectAddressActivity.start(ExchangeRecordActivity.this);
    }
}
